let storage = bui.storage();
let language,translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#commentListLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                autoClose: false,
                text:"加载中。。。",
                callback: function (argument) {
                }
            });

            router.$("#commentFh").click(function () {
                storage.set("tzPage", '0')
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            })

            let token = storage.get("token",0)
            if (token) {
                // uiLoading.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        bui.confirm({
                            "title": "",
                            "height": 460,
                            "content": '<div class="bui-box-center"><div><h3>登录已过期</h3></div></div>',
                            "buttons": [{ name: "确定", className: "primary-reverse" }],
                            callback:function(){
                                this.close();
                                storage.set("dlTz", '12')
                                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                            }
                        });
                    }
                })

                let uiList = bui.list({
                    id: "#commentScrollList",
                    url: global.api.getCommentList,
                    pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
                    data: {},
                    headers: {Token: token},
                    //如果分页的字段名不一样,通过field重新定义
                    field: {
                        page: "page",
                        size: "limit",
                        data: "data"
                    },
                    callback: function(e) {
                        // e.target 为你当前点击的元素
                        // e.currentTarget 为你当前点击的handle 整行
                        let cla = router.$(e.target).attr("class")
                        let id = router.$(e.target).attr("name")
                        let user = router.$(e.target).attr("user")

                        if (cla) {
                            if (cla.indexOf("delete") > -1) {
                                //删除
                                bui.confirm("确认要删除此留言么？", function(e) {
                                    //this 是指点击的按钮
                                    let text = router.$(e.target).text();

                                    if (text == "确定") {
                                        uiLoading.show()
                                        bui.ajax({
                                            url: global.api.deleteComment,
                                            data:{id: id},
                                            method: "POST"
                                        }).then(function(res){
                                            if (res.code == '200') {
                                                uiList.refresh();
                                                bui.confirm({
                                                    "title": "",
                                                    "height": 460,
                                                    "content": '<div class="bui-box-center"><div><i class="icon-successfill success"></i><h3>删除成功</h3></div></div>',
                                                    "buttons": [{ name: "我知道了", className: "primary-reverse" }]
                                                });
                                            }
                                            uiLoading.stop()
                                        })
                                    }
                                    this.close()
                                });
                            } else if (cla.indexOf("auditSuccess") > -1) {
                                //审核通过
                                bui.confirm("确认要审核通过此信息么？", function(e) {
                                    //this 是指点击的按钮
                                    let text = router.$(e.target).text();

                                    if (text == "确定") {
                                        uiLoading.show()
                                        bui.ajax({
                                            url: global.api.auditComment,
                                            data: {id: id, status: '1', user: user},
                                            method: "POST"
                                        }).then(function(res){
                                            if (res.code == '200') {
                                                uiList.refresh();
                                                bui.confirm({
                                                    "title": "",
                                                    "height": 460,
                                                    "content": '<div class="bui-box-center"><div><i class="icon-successfill success"></i><h3>审核通过成功</h3></div></div>',
                                                    "buttons": [{ name: "我知道了", className: "primary-reverse" }]
                                                });
                                            } else {
                                                bui.alert(res.msg)
                                            }
                                            uiLoading.stop()
                                        })
                                    }
                                    this.close()
                                });
                            }else if (cla.indexOf("auditFauit") > -1) {
                                bui.prompt("请输入拒绝原因",function(e){
                                    let text = router.$(e.target).text();
                                    if( text == "取消"){
                                        this.close();
                                    }else{
                                        let thi = this
                                        uiLoading.show()
                                        bui.ajax({
                                            url: global.api.auditComment,
                                            data: {id: id, status: '3', error: this.value(), user: user},
                                            method: "POST"
                                        }).then(function(res){
                                            if (res.code == '200') {
                                                thi.close();
                                                bui.confirm({
                                                    "title": "",
                                                    "height": 460,
                                                    "content": '<div class="bui-box-center"><div><i class="icon-successfill success"></i><h3>审核拒绝成功</h3></div></div>',
                                                    "buttons": [{ name: "我知道了", className: "primary-reverse" }]
                                                });
                                            } else {
                                                bui.alert(res.msg)
                                            }
                                            uiLoading.stop()
                                        })
                                    }
                                })
                            }else if (cla.indexOf("article") > -1) {
                                storage.set("articleLookId", id)
                                router.load({ url: "pages/articleLook/articleLook.html", param: {tzSign: '2'}});
                            }
                        }
                    },
                    template: function(data) {
                        let fontSize = '0.26rem'
                        if (language !== "chinese_simplified") {
                            fontSize = '0.2rem'
                        }

                        let html = "";
                        data.forEach(function(el, index) {
                            html += '<li class="bui-btn bui-box">\n' +
                                '                        <div class="span1">\n' +
                                '                            <p style="color: black;font-size: 0.28rem;margin-bottom: 0.22rem">'+el.content+'</p>\n' +
                                '                            <p class="item-text" style="font-size: '+fontSize+'"><span class="bui-label">留言文章</span><span class="bui-value article" name="'+el.pid+'">'+el.pidStr+'</span></p>\n'+
                                '                            <p class="item-text" style="font-size: '+fontSize+'"><span class="bui-label">留言时间</span><span class="bui-value">'+formatDate(el.ctime)+'</span></p>\n' +
                                '                            <p class="item-text" style="font-size: '+fontSize+'"><span class="bui-label">留言用户</span><span class="bui-value">'+el.userStr+'</span></p>\n'

                            let statusStr = ''
                            let btnStr = ''
                                if(el.status == '3') {
                                    statusStr = '审核失败'
                                    if (el.error) {
                                        statusStr +=  '(' + el.error + ')'
                                    }
                                    btnStr =  '                            <div class="bui-box">\n' +
                                        '                                <div class="span1">\n' +
                                        '                                    <div class="czbtn delete" style="background: red;" name="'+el.id+'">\n' +
                                        '                                        删除\n' +
                                        '                                    </div>\n' +
                                        '                                </div>\n'+
                                        '                                <div class="span1">\n' +
                                            '                                </div>\n'+
                                            '                                <div class="span1">\n' +
                                            '                                </div>\n'
                                    btnStr += '                        </div>\n'
                                } else if (el.status == '1') {
                                    statusStr = '展示中'
                                    btnStr =  '                            <div class="bui-box">\n' +
                                        '                                <div class="span1">\n' +
                                        '                                    <div class="czbtn delete" style="background: red;" name="'+el.id+'">\n' +
                                        '                                        删除\n' +
                                        '                                    </div>\n' +
                                        '                                </div>\n'+
                                        '                                <div class="span1">\n' +
                                        '                                </div>\n'+
                                        '                                <div class="span1">\n' +
                                        '                                </div>\n'
                                    btnStr += '                        </div>\n'
                                } else {
                                    statusStr = '待审核'
                                    btnStr =  '                            <div class="bui-box">\n' +
                                        '                                <div class="span1">\n' +
                                        '                                    <div class="czbtn delete" style="background: red;" name="'+el.id+'">\n' +
                                        '                                        删除\n' +
                                        '                                    </div>\n' +
                                        '                                </div>\n'+
                                        '                                <div class="span1">\n' +
                                            '                                    <div class="czbtn auditSuccess" style="background:#3da1ff;" name="'+el.id+'" user="'+el.user+'">\n' +
                                            '                                        审核通过\n' +
                                            '                                    </div>\n' +
                                            '                                </div>\n'+
                                        '                                <div class="span1">\n' +
                                        '                                    <div class="czbtn auditFauit" style="background: #ff841d;" name="'+el.id+'" user="'+el.user+'">\n' +
                                        '                                        审核拒绝\n' +
                                        '                                    </div>\n' +
                                        '                                </div>\n' +
                                        '                        </div>\n'
                                }

                            html += '                            <p class="item-text" style="font-size: '+fontSize+'"><span class="bui-label">状态</span><span class="bui-value" style="color: red">'+statusStr+'</span></p>\n'+
                                btnStr+'                    </li>'
                        });
                        uiLoading.stop()

                        setTimeout(function () {
                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        },1000)
                        return html;
                    }
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '12')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }
            try {
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function formatDate(time) {
    let date = new Date(time * 1000);
    // let now = new Date();
    // let days=parseInt((now - date) / (1000 * 60 * 60* 24) + '');
    // if (days > 7){
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() < 10+1 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s
    // } else if (days < 1) {
    //     let hour=parseInt((now - date) / (1000 * 60 * 60) + '');
    //     return hour + "小时前"
    // }
    // return days + "天前"
}
